﻿using DataProcess.Core.DataAccess;
using DataProcess.Core.Enums;
using DataProcess.Core.Model;
using DataProcess.Core.Tools;
using DataProcess.Interface;
using DataProcess4Net48.CustomUserControl;
using DataProcess4Net48.Views;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace DataProcess4Net48
{
    public partial class MainForm : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        bool expanded = false;
        bool locked = false;

        public MainForm()
        {
            InitializeComponent();
        }
        private void MainForm_Load(object sender, EventArgs e)
        {
            var equipments = new DbContext<Equipment>().CurrentDb.GetList();

            SetFormSize(equipments.Count);

            if (equipments != null && equipments.Count > 0)
            {
                foreach (var item in equipments)
                {
                    EquipmengControl ctrl = new EquipmengControl(item);
                    EquipmentLayout.Controls.Add(ctrl);
                }
            }

            var sys = new DbContext<SystemSetting>().CurrentDb.GetList();
            if (sys.Count > 0)
            {
                Program.SystemSetting = sys[0];
            }

            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ReflectionMark.FormStateFileName);
            if (File.Exists(path))
            {
                string context = File.ReadAllText(path);
                var fs = context.FromJson<FormState>();
                if (fs != null)
                {
                    this.Location = fs.Location;
                    if (fs.Expaned)
                    {
                        BBI_Expand_ItemClick(null, null);
                    }
                    if (fs.Locked)
                    {
                        TopMost = true;
                    }
                }
            }
        }

        private void SetFormSize(int count)
        {
            if (count == 1)
            {
                //445  276
                this.Size = new Size(445, 276);
                this.Size = new Size(434, 271);
            }
            else if (count == 2)
            {
                //this.Size = new Size(878, 276);
                this.Size = new Size(878, 271);
            }
            else if (count > 2)
            {
                //this.Size = new Size(878, 276);
                this.Size = new Size(878, 271);
            }
        }

        private void SystemSettingUpdated(SystemSetting setting)
        {
            Program.SystemSetting = setting;
        }

        private void EquipmentAdded(Equipment newItem)
        {
            if (newItem == null) return;
            EquipmengControl ctrl = new EquipmengControl(newItem);
            EquipmentLayout.Controls.Add(ctrl);
            SetFormSize(EquipmentLayout.Controls.Count);
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {

        }

        private void BSI_SystemSetting_ItemClick(object sender, ItemClickEventArgs e)
        {
            SystemSettingForm frm = new SystemSettingForm();
            frm.Updated += SystemSettingUpdated;
            if (frm.ShowDialog(this) == DialogResult.OK) { }
        }


        private void BSI_AddEquipment_ItemClick(object sender, ItemClickEventArgs e)
        {
            EquipmentDetailsForm frm = new EquipmentDetailsForm(new Equipment(), DataEditType.Add)
            {
                Text = "添加设备"
            };
            frm.Added += EquipmentAdded;
            frm.ShowDialog(this);
        }

        private void BBI_Expand_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (expanded)
            {
                Height = 276;
                BBI_Expand.ImageOptions.Image = Properties.Resources.unexpand;
                BBI_Expand.Hint = "点击收缩";
            }
            else
            {
                Height = 39;
                BBI_Expand.ImageOptions.Image = Properties.Resources.expand;
                BBI_Expand.Hint = "点击展开";
            }
            expanded = !expanded;
        }

        private void BBI_FixedTop_ItemClick(object sender, ItemClickEventArgs e)
        {
            //434,271
            TopMost = !locked;
            if (locked)
            {
                FormBorderStyle = FormBorderStyle.FixedSingle;
                BBI_FixedTop.ImageOptions.Image = Properties.Resources.unlock;
                BBI_FixedTop.Hint = "点击锁定窗体位置并置顶";
                if (EquipmentLayout.Controls.Count > 1)
                {
                    Width = 1000;
                    Width = 878;
                }
            }
            else
            {
                FormBorderStyle = FormBorderStyle.None;
                BBI_FixedTop.ImageOptions.Image = Properties.Resources._lock;
                BBI_FixedTop.Hint = "点击解锁可移动窗体并取消置顶";

                //获取窗体现在的大小，位置，为下次软件重启找到位置
                SaveFormState();
            }
            locked = !locked;
        }

        private void SaveFormState()
        {
            FormState fs = new FormState
            {
                Size = this.Size,
                Location = Location,
                Locked = locked,
                Expaned = expanded
            };
            try
            {
                File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ReflectionMark.FormStateFileName), fs.ToJson());
            }
            catch
            {
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (XtraMessageBox.Show(this, "您确定关闭此软件么，关闭之后无法取得数据进行传输", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void TimerRunningState_Tick(object sender, EventArgs e)
        {
            foreach (var ctrl in EquipmentLayout.Controls)
            {
                if (ctrl is EquipmengControl ec)
                {
                    ec.UpdateRunningState();
                }
            }
        }
    }

    public class FormState
    {
        public Size Size { get; set; }

        public Point Location { get; set; }

        public bool Locked { get; set; }

        public bool Expaned { get; set; }
    }
}